package panels;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.rmi.RemoteException;

import javax.swing.JPanel;

public class SeaPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	public static int ROW_SIZE = 10;
    public static int SIZE = 10;
    public static int PLAYER_AMOUNT = 4;
    public static int SHIP_AMOUNT = 4;
    private boolean canShoot = true;
    private int[][] compareTab= new int[ROW_SIZE*SIZE*PLAYER_AMOUNT/2][ROW_SIZE*SIZE*PLAYER_AMOUNT/2];
    public  void setCompareTab(int tab[][])
    {
    	this.compareTab = tab;
    }
    
	private int ID;
	public boolean isDead;
	public boolean getState()
	{
		return isDead;
	}
    public void setID(int id)
    {
    	this.ID = id;
    }
    public int getID()
    {
    	return ID;
    }
    public synchronized boolean isCanShoot() {
        return canShoot;
    }

    public synchronized void setCanShoot(boolean canShoot) {
        this.canShoot = canShoot;
    }
    private int shootCount =0;
    public int[][] mainTab = new int[ROW_SIZE*SIZE*PLAYER_AMOUNT/2][ROW_SIZE*SIZE*PLAYER_AMOUNT/2];

    public synchronized int[][] getTab()
    {
        return mainTab;
    }
    
    public SeaPanel() {
        fillTable(0);
        initComponents();
       
        
    }
    
    public SeaPanel(int i) {
        if(i!=0 )
        {
        fillTable(0);
        initComponents();
       
        }
        
    }
   
    
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        this.setSize(200, 200);
//        g.setColor(Color.WHITE);
//        g.fillRect(0, 0, ROW_SIZE*SIZE*PLAYER_AMOUNT/2, ROW_SIZE*SIZE*PLAYER_AMOUNT/2);
        drawSea(g);
        drawBorder(g);
        try {
			drawEnd(g);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      //  mainTab[1][1]=1;

        
    }
    private void drawEnd(Graphics g) throws RemoteException {
    	int c1,c2,c3,c4;
    	c1=0;c2=0;c3=0;c4=0;
		 for(int i=0;i<10;i++)
	           for(int j=0;j<10;j++)
	        	   if(compareTab[i][j]==2)
	        		   c1++;
		 for(int i=10;i<20;i++)
	           for(int j=0;j<10;j++)
	        	   if(compareTab[i][j]==2)
	        		   c2++;
		 for(int i=10;i<20;i++)
	           for(int j=10;j<20;j++)
	        	   if(compareTab[i][j]==2)
	        		   c3++;
		 for(int i=0;i<10;i++)
	           for(int j=10;j<20;j++)
	        	   if(compareTab[i][j]==2)
	        		   c4++;


		 if((c1==4 && ID==1) || (c2==4 && ID==2)
			 || (c3==4 && ID==3)  || (c4==4 && ID==4) )
			 isDead=true;
		 if((c1>=4 && ID !=1) ||  ( isDead && ID==1))
		 {
			 g.fillRect(0,0,100,100);
			
		 }
		 if((c2>=4 && ID!=2)  || ( isDead && ID==2))
		 {
			 g.fillRect(100,0,200,100);
			
		 }
		 if((c3>=4 && ID !=3)  || ( isDead && ID==3))
		 {
			 g.fillRect(100,100,200,200);
			
		 }
		 if((c4>=4 && ID !=4)  || ( isDead && ID==4))
		 {
			 g.fillRect(0,100,100,200);
		 }
		 
	}

	public void drawBorder(Graphics g)
    {
        g.setColor(Color.BLACK);
        Graphics2D g2= (Graphics2D) g;
        Stroke thin = new BasicStroke(1);
        Stroke thick = new BasicStroke(2);
        for(int i=0;i<=ROW_SIZE*SIZE*PLAYER_AMOUNT/2;i+=ROW_SIZE)
        {
            if(i==ROW_SIZE*SIZE*PLAYER_AMOUNT/4  || i==0 || i==ROW_SIZE*SIZE*PLAYER_AMOUNT/2)
                g2.setStroke(thick);
            else 
                g2.setStroke(thin);
            g2.drawLine(i,0,i,ROW_SIZE*SIZE*PLAYER_AMOUNT/2);
            g2.drawLine(0,i,ROW_SIZE*SIZE*PLAYER_AMOUNT/2,i);
        }
    }
    public void drawSea(Graphics g)
    {
        
        for(int i=0;i<mainTab.length;i++)
           for(int j=0;j<mainTab.length;j++)
           {
               g.setColor(getFieldColor(mainTab[i][j]));
               g.fillRect(i*ROW_SIZE, j*ROW_SIZE, i*ROW_SIZE+ROW_SIZE, j*ROW_SIZE + ROW_SIZE);
           }
    }
    public void fillTable(int value)
    {
       for(int i=0;i<mainTab.length;i++)
           for(int j=0;j<mainTab.length;j++)
            {
                mainTab[i][j]=value;
            }
        
    }
    
    public Color getFieldColor(int i)
    {
        if(i==0)
        return Color.WHITE;
        else if(i==1)
            return Color.BLACK;
        else if(i==2 ) return Color.RED;
        else if (i==3) return Color.BLUE;
        else return Color.YELLOW;
    }
    
    public static int cutNumber(int i)
    {
        if(i>=10 && i<=20)i-=10;
        
        
        return i;
    }
    public static String mapField(int j, int i)
    {
    	if(i>99) i-=100;
    	if(j>99) j-=100;
        StringBuffer shoot= new StringBuffer();   
        int a= i/ROW_SIZE;
        int b= j/ROW_SIZE;
        a=cutNumber(a);
        b=cutNumber(b);
       // i%=10;       
      //  j%=10;
       // System.out.println(i +" " + j);
        switch(b)
        {
            case 0: shoot.append("A"); break;
            case 1: shoot.append("B"); break;
            case 2: shoot.append("C"); break;
            case 3: shoot.append("D"); break;
            case 4: shoot.append("E"); break;
            case 5: shoot.append("F"); break;
            case 6: shoot.append("G"); break;
            case 7: shoot.append("H"); break;
            case 8: shoot.append("I"); break;
            case 9: shoot.append("J"); break;
        }
        shoot.append(a+1);
        return shoot.toString();
        
        
        
    }
    public synchronized int getShootCount()
    {
        return shootCount;
    }
    public synchronized void setShootCount(int i)
    {
        this.shootCount=i;
    }
    
    public void initComponents()
    {
    	 setPreferredSize(new java.awt.Dimension(200, 200));

         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
         this.setLayout(layout);
         layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGap(0, 201, Short.MAX_VALUE)
         );
         layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGap(0, 201, Short.MAX_VALUE)
         );
    }

}
